import { ValidateErrorInfo } from '@/ts/base/model';
export function mapErrorToValidateInfo(error: any, position?: string): ValidateErrorInfo {
  let msg = String(error);
  if (error instanceof Error) {
    msg = error.message;
    let match: RegExpExecArray | null;
    if ((match = /^(.+) is not defined/.exec(msg))) {
      msg = `变量或函数 '${match[1]}' 没有定义`;
    } else if ((match = /Unexpected token.+/.exec(msg))) {
      msg = '语法错误：' + msg;
    } else if ((match = /Cannot read properties of undefined \(reading '(.+)'\)/.exec(msg))) {
      msg = `尝试对空值读取属性 '${match[1]}'`;
    }
  }

  return {
    errorCode: 'ERR_FAILED',
    errorLevel: 'error',
    message: `错误：${msg} \n请检查配置`,
    position,
  };
}
